const SitesModel = require('@/models/sites.model')
const JsonResult = require('@/utils/httpResponse.unit')
const { getSessionuserId } = require('@/utils/common.util')
const SiteFolderModel = require('@/models/site_folder.model')
const UserCollectToolModel = require('@/models/user_collect_tool.model')

class SiteFolderController {
  async findFolders (req, response) {
    try {
      const userId = req.query.userId
      if (!userId) {
        return JsonResult.fail({ req, response, message: '缺少userId' })
      }
      const sessionuserId = await getSessionuserId(req)
      const data = await SiteFolderModel.find({
        userId,
        isPrivate: userId !== sessionuserId ? '0' : undefined
      })
      JsonResult.success({
        req,
        data,
        response,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询收藏夹列表失败' })
    }
  }

  async createFolder (req, response) {
    try {
      const { name, isPrivate } = req.body
      const userId = await getSessionuserId(req)
      if (await SiteFolderModel.findOne({ userId, name })) {
        return JsonResult.fail({ req, response, message: '文件夹名称不能重复' })
      }
      await SiteFolderModel.add({ userId, name, isPrivate })
      JsonResult.success({
        req,
        response,
        message: '创建收藏夹成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '创建收藏夹失败' })
    }
  }

  async updateFolder (req, response) {
    try {
      const { id, name, isPrivate } = req.body
      const sessionuserId = await getSessionuserId(req)
      const folder = await SiteFolderModel.findOne({ id })
      if (!folder) {
        return JsonResult.fail({ req, response, message: '收藏夹不存在' })
      }
      if (folder.userId !== sessionuserId) {
        return JsonResult.httpStatus(req, response, 403, {
          message: 'No permission to update folder',
          code: '9999'
        })
      }
      // 查询该用户下指定名称的文件夹检测文件夹名称是否重复
      const exist = await SiteFolderModel.findOne({ userId: sessionuserId, name })
      if (exist && exist.id !== id) {
        return JsonResult.fail({ req, response, message: '文件夹名称不能重复' })
      }
      await SiteFolderModel.update({ id, name, isPrivate })
      JsonResult.success({
        req,
        response,
        message: '更新收藏夹成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '更新收藏夹失败' })
    }
  }

  async deleteFolder (req, response) {
    try {
      const id = req.body.id
      const userId = await getSessionuserId(req)
      const folder = await SiteFolderModel.findOne({ id })
      if (!folder) {
        return JsonResult.fail({ req, response, message: '收藏夹不存在' })
      }
      if (folder.userId !== userId) {
        return JsonResult.httpStatus(req, response, 403, {
          message: 'No permission to delete folder',
          code: '9999'
        })
      }
      await SitesModel.delete({ folderId: id })
      await SiteFolderModel.delete(id)
      JsonResult.success({
        req,
        response,
        message: '删除收藏夹成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '删除收藏夹失败' })
    }
  }

  async updateSort (req, response) {
    try {
      const ids = req.body.ids
      const sessionuserId = await getSessionuserId(req)
      await SiteFolderModel.updateSort(ids, sessionuserId)
      JsonResult.success({
        req,
        response,
        message: '更新排序成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '排序失败' })
    }
  }
}

module.exports = new SiteFolderController()
